<?php

function mcymna_calendar_display() {
	$mo = arg(3);
	$yr = arg(2);
	if(empty($mo) || !is_numeric($mo)) {
		$mo = intval(date('m'));
	}
	if(empty($yr) || !is_numeric($yr)) {
		$yr = intval(date('Y'));
	}
	$sql= "SELECT n.nid,n.title,nr.teaser,ctst.field_start_value
	FROM {node} AS n
	INNER JOIN {node_revisions} AS nr ON (nr.vid=n.vid)
	INNER JOIN {content_type_special_event} AS ctst ON (nr.vid=ctst.vid)
	WHERE n.status=1 AND ctst.field_start_value >='%d-%d-01' AND ctst.field_start_value <'%d-%d-01'";
	if($mo==12) {
		$yr2=$yr+1;
		$mo2=1;
		$mo3=$mo-1;
		$yr3=$yr;
	}
	else if($mo==1) {
		$mo2=$mo+1;
		$yr2=$yr;
		$mo3=12;
		$yr3=$yr-1;
	}
	else {
		$mo2=$mo+1;
		$yr2=$yr;
		$mo3=$mo-1;
		$yr3=$yr;
	}
	$items = db_query($sql,array($yr,$mo,$yr2,$mo2));
	$startdate = $yr."-".$mo."-01";
	$html="";
	$html.= "<h3>".date("F Y",strtotime($startdate))."</h3>";
	$html.="<div class='full-calendar'>";
	$html.= "<div class='controls'><a href='/events/calendar/".$yr3."/".$mo3."'>Previous</a>&nbsp;&nbsp;&nbsp;<a href='/events/calendar/".$yr2."/".$mo2."'>Next</a></div>";
	
	$html.="<dl class='calendar_list'>";
	$norows=true;
	while(($row=db_fetch_array($items))) {
		$norows=false;
		$html .= "<dt><a href=\"/node/".$row['nid']."\">".$row['title']."</a> - ".date('m-d-Y',strtotime($row['field_start_value']))."</dt>";
		$html .= "<dd>".$row['teaser']." - <a href=\"/node/".$row['nid']."\">Read More</a></dd>";
	}
	if($norows==true) {
		$html.="<dt>There are no special events this month.</dt>";
	}
	$html.="</dl>";
	$html.="</div>";
//	$prefix = mcymna_contrib_block_contents("mcymna-events");
	return $html;
}
